package com.pp.controller;

import com.github.pagehelper.PageInfo;
import com.pp.entity.User;
import com.pp.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * Created by lanou on 18/2/27.
 */
@Controller
@RequestMapping("/success")
public class SuccessController {
    @Resource
    private UserService userService;
    @RequestMapping("/index")
    public ModelAndView index(ModelAndView mav, Integer pageNum){
        if(pageNum == null) {
            pageNum = 1;
        }
        PageInfo pageInfo =  userService.findAllUser(pageNum);
        mav.addObject("users",pageInfo);
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public void delete(User user){
        userService.delete(user);
    }
    @RequestMapping("/omnipotent")
    public ModelAndView omnipotent(HttpSession session,ModelAndView mav, User user){
        User user1 = userService.updateUser(user);
        mav.addObject("user",user1);
        session.setAttribute("uid",user.getId());
        mav.setViewName("omnipotent");
        return mav;
    }
    @RequestMapping("revise")
    public void revise(User user,HttpSession session){
        int id = (int)session.getAttribute("uid");
        userService.upUser(user,id);
    }
}
